package com.qyj.spring_boot_02.controller;

import com.alibaba.fastjson.JSONObject;
import com.qyj.spring_boot_02.bean.user.LoginVo;
import com.qyj.spring_boot_02.bean.user.User;
import com.qyj.spring_boot_02.service.UserService;
import com.qyj.spring_boot_02.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserContraller {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public LoginVo login(@RequestBody LoginVo loginVo){
        if (loginVo ==null || loginVo.getName().equals("") || loginVo.getPassword().equals("")){
            loginVo = new LoginVo();
            loginVo.setStatusCode(-1);
            return loginVo;
        }
        if (loginVo.getName() != null && loginVo.getLoginTime()!=null){
            User user = userService.getUser(loginVo.getName(), loginVo.getPassword());
            if (user != null){
                loginVo.setMail(user.getMail());
                loginVo.setPassword("**********");
                String token = TokenUtil.sign(loginVo.getName(), loginVo.getLoginTime());
                log.info("登录token："+token);
                loginVo.setStatusCode(1);
                loginVo.setToken(token);
            }else {
                User qquser = userService.getqqUser(loginVo.getName(),loginVo.getPassword());
                if (qquser != null){
                    loginVo.setMail(qquser.getMail());
                    loginVo.setPassword("**********");
                    loginVo.setName(qquser.getName());
                    String token = TokenUtil.sign(qquser.getName(), loginVo.getLoginTime());
                    log.info("登录token："+token);
                    loginVo.setStatusCode(1);
                    loginVo.setToken(token);
                }else{
                    loginVo = new LoginVo();
                    loginVo.setStatusCode(-1);
                    return loginVo;
                }

            }
        }else {
            loginVo = new LoginVo();
            loginVo.setStatusCode(-1);
            return loginVo;
        }
        return loginVo;
    }
    // todo 注册
    @PostMapping("/register")
    public JSONObject register(@RequestBody LoginVo loginVo){

        if(loginVo==null){
            JSONObject json = new JSONObject();
            json.put("success", -1);
            json.put("msg", "注册失败");
            json.put("code", "500");
            return json;
        }
        String name = loginVo.getName();
        String password = loginVo.getPassword();
        String mail = loginVo.getMail();
        String code = loginVo.getCode();
        if( name.equals("") || password.equals("")
                || mail.equals("") || code.equals("")){
            JSONObject json = new JSONObject();
            json.put("success", -1);
            json.put("msg", "注册失败");
            json.put("code", "500");
            return json;
        }
        return userService.saveUser(code,name,password,mail);
    }
    @GetMapping("/getcode")
    public JSONObject getCode(@RequestParam("name") String name,@RequestParam("mail") String mail){
        log.info("用户名："+name+" 邮箱："+mail);
        boolean t = userService.getCode(name,mail);
        if(t){
            JSONObject json = new JSONObject();
            json.put("success", 1);
            json.put("msg", "发送验证码成功");
            json.put("code", "200");
            return json;
        }
        JSONObject json = new JSONObject();
        json.put("success", -1);
        json.put("msg", "验证码发送失败,检查邮箱是否正确！！");
        json.put("code", "500");
        return json;
    }
    // 找回密码
    @PostMapping("/wangjipwd")
    public LoginVo getPwd(@RequestBody LoginVo loginVo){
        return userService.getPwd(loginVo);
    }
}
